Public Class frmBaoCaoHangNhapXuatTheoTCNX
    Dim TCNX As String = ""
    
    Private Sub frmBaoCaoHangNhapXuatTheoTCNX_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rbtSLKH.Checked = True
        cbbLoaiBaocao.SelectedIndex = 0
        KhoiTaocbbKyQuyetToan()
        cbbKYQT.SelectedIndex = 0
    End Sub

    Public Sub KhoiTaocbbKyQuyetToan()
        Dim ds As DataSet = BaseDB.ExecSql_DataSet("select * from tblKyQT")
        cbbKyQT.DisplayMember = "tenkyqt"
        cbbKyQT.ValueMember = "id"
        cbbKyQT.DataSource = ds.Tables(0)
    End Sub

    Private Sub txtTinhChatNhapXuat_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTinhChatNhapXuat.DoubleClick
        If (cbbLoaiBaocao.SelectedIndex = 0) Then
            ShowFormDoiTuong(enumDOITUONG.TINHCHATNHAP)
        Else 'xuat
            ShowFormDoiTuong(enumDOITUONG.TINHCHATXUAT)
        End If
    End Sub
    Private Sub txtTinhChatNhapXuat_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTinhChatNhapXuat.KeyDown
        If (e.KeyCode = Keys.F1) Then
            txtTinhChatNhapXuat_DoubleClick(txtTinhChatNhapXuat, Nothing)
        ElseIf (e.Modifiers = Keys.Control And e.KeyCode = Keys.Delete) Then
            TCNX = ""
            txtTinhChatNhapXuat.Text = ""
        End If
    End Sub

    Public Sub ShowFormDoiTuong(ByVal loai As enumDOITUONG)
        Dim frm As New frmDMDoiTuong
        frm.bCallForm = True
        frm.eDoiTuong = loai
        frm.ShowDialog()
        If frm.selectedDT IsNot Nothing Then
            TCNX = frm.selectedDT.KH
            txtTinhChatNhapXuat.Text = frm.selectedDT.Ten
        Else
            TCNX = ""
            txtTinhChatNhapXuat.Text = ""
        End If
    End Sub

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        If (TCNX = "") Then
            ShowMessages("Bạn chưa chọn TCNX")
            Exit Sub
        End If
        '
        Dim strsql, nx, tieude As String
        If (cbbLoaiBaocao.SelectedIndex = 0) Then
            nx = "N"
            tieude = "BÁO CÁO HÀNG NHẬP THEO "
        Else
            nx = "X"
            tieude = "BÁO CÁO HÀNG XUẤT THEO "
        End If
        tieude &= txtTinhChatNhapXuat.Text.ToUpper
        tieude &= Chr(10) & cbbKYQT.Text.ToUpper
        Dim ds As DataSet
        Dim loai_SLBaoCao As String
        If (rbtSLKH.Checked) Then
            loai_SLBaoCao = "SLKH"
        Else
            loai_SLBaoCao = "SLTH"
        End If
        '
        Dim KyQT As String
        KyQT = cbbKYQT.SelectedValue

        strsql = "select * from viewBaoCaoHangMua where NX= '" & nx & "' " & _
                 " and KYQT = '" & KyQT & "'" & _
                 " and tcnx = '" & TCNX & "'" & _
                 " order by DVGN, SOPHIEU"

        ds = BaseDB.ExecSql_DataSet(strsql)
        Dim tongtien As Double
        Dim row As DataRow
        For Each row In ds.Tables(0).Rows
            If (rbtSLKH.Checked) Then
                tongtien += row.Item("gle") * row.Item("SLKH")
            Else
                tongtien += row.Item("gle") * row.Item("SLTH")
            End If
        Next
        tongtien = Math.Round(tongtien)
        Dim nnreport As New rptBaoCaoHangMua
        nnreport.SetDataSource(ds.Tables(0))
        nnreport.SetParameterValue(0, loai_SLBaoCao)
        nnreport.SetParameterValue("tien", DoiSoRaChu(tongtien))
        nnreport.SetParameterValue("tieude", tieude)
        Dim frm As New frmReportViewer(nnreport)
        frm.ShowDialog()
    End Sub

    
End Class